const request=require('../../util/request.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    order_time:"请选择订单日期",//订单日期
    mindate:Math.round((new Date() - 365*86400*1000)),
    p_name:"",//项目名称
    customer_name:"",//客户名称
    delivery_time:"请选择交货日期",//交货日期
    desc:"",//备注
    p_no:"",//项目号
    show:false,
    date:"",
    showstarttime:false,
    showlasttime:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this 
    var params = {
      p_no:options.p_no,
    }
    that.setData({p_no:options})
    request.requestGet('project/edit_info', params, '',function (res) {
      that.setData({
        order_time:res.data.project_info.order_time,
        p_name:res.data.project_info.p_name,
        customer_name:res.data.project_info.customer_name,
        delivery_time:res.data.project_info.delivery_time,
      })
      if(res.data.project_info.desc === null){
      }else{
        that.setData({
          desc:res.data.project_info.desc,
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  submit(event){
    var that = this
    let tmpinfo = {
      p_no:that.data.p_no.p_no,
      order_time:that.data.order_time,
      delivery_time:that.data.delivery_time,
      customer_name:that.data.customer_name,
    }
    request.requestAjax('project/edit_info',tmpinfo,"提交中",function(res){
      if(res.status==200){
          // wx.navigateBack()
          const pages = getCurrentPages();
          if (pages.length >= 2) {
            const prevPage = pages[pages.length - 2];
            pages[pages.length - 2].setData({
              __returnParams: true
            }, () => {

              wx.showToast({
                title: '提交成功',
                icon: 'success',
              })
              setTimeout(() => {
                wx.navigateBack() // 确保setData回调完成后再返回
                prevPage.onLoad({}); // 触发上一页自定义刷新方法
              }, 1000)
             
            })
          }
          return false;
      }else{
        wx.showToast({
          title: res.msg,
          icon:"none"
        })
      }

  },function(res){
    wx.showToast({
      title: '请求失败',
      icon:"none"
    })
  });
  },
  bindKeyInputVant(event){
    let valueName = event.target.dataset.variable;
    this.setData({
      [valueName]:event.detail
    });
  },
  onDisplay() {
    this.setData({ show: true });
  },
  onClose() {
    this.setData({ showstarttime: false });
    this.setData({ showlasttime: false });
  },
  onConfirm(event) {
    this.setData({
      show: false,
      date: this.formatDate(event.detail[0])+' - '+this.formatDate(event.detail[1]),
      findtime : this.formatDate(event.detail[0])+' - '+this.formatDate(event.detail[1])
    });
  },
  formatDate(date) {
    date = new Date(date);
    return `${date.getFullYear()}-${date.getMonth()+1}-${date.getDate()}`;
  },
  showstart() {
    this.setData({ showstarttime: true });
  },
  
  showlast() {
    this.setData({ showlasttime: true });
  },

  start(event) {
    const tmptime = this.formatDate(event.detail)
    this.setData({
      order_time:tmptime
    })
    this.onClose()
  },

  last(event) {
    const tmptime = this.formatDate(event.detail)
    this.setData({
      delivery_time:tmptime
    })
    this.onClose()
  },
  
})